/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.comandos.admin;

import br.ufc.quixada.adrs.interfaces.Comando;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.service.AdrsService;
import br.ufc.quixada.adrs.util.Msg;
import br.ufc.quixada.adrs.util.Paginacao;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ismaily
 */
public class CmdExcluirAdrs implements Comando {

    public String executa(HttpServletRequest request, HttpServletResponse response) {
        AdrsService as = new AdrsService();
        HttpSession session = request.getSession(true);

        Long id = Long.parseLong(request.getParameter("id"));
        Adrs adrs = as.getAdrsById(id);

        if (adrs.getProdutores() != null && !adrs.getProdutores().isEmpty()) {
            session.setAttribute("erro", Msg.ADRS_STILL_CONTAINS_PRODUTORES);
        } else {
            if (as.deleteAdrs(adrs)) {
                session.setAttribute("sucesso", Msg.SUCCESSFULLY_DELETED);
                //session.setAttribute("listaAdrs", as.getAllAdrs());
            } else {
                session.setAttribute("erro", Msg.OPERATIONAL_ERROR_DELETING_ADRS);
            }
            Paginacao.atualizarListaAdrs(as, session, 1l, 0l);
        }
        return "/admin/admin_gerenciar_adrs.jsp";
    }
}
